package com.lc1993929.mr.rating;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * Created by LiuChang on 2018/1/18.
 */
public class HotMoviesTopN {
    static class HotMoviesTopNMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
        ObjectMapper objectMapper;
        Text k = new Text();
        IntWritable v = new IntWritable();

        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            objectMapper = new ObjectMapper();
        }

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            RateBean bean = objectMapper.readValue(value.toString(), RateBean.class);
            k.set(bean.getMovie());
            context.write(k, v);
        }
    }


    static class HotMoviesTopNReducer extends Reducer<Text, IntWritable, SortBean, NullWritable> {
        private TreeMap<SortBean, String> hotMovies = new TreeMap<>();

        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

            int count = 0;
            for (IntWritable value : values) {
                count += value.get();
            }
            SortBean sortBean = new SortBean();
            sortBean.setMovie(key.toString());
            sortBean.setCounts(count);
            hotMovies.put(sortBean, null);


        }

        @Override
        protected void cleanup(Context context) throws IOException, InterruptedException {
            int topn = context.getConfiguration().getInt("movie.hot.top.n", 10);
            Set<Map.Entry<SortBean, String>> entrySet = hotMovies.entrySet();
            int i = 0;
            for (Map.Entry<SortBean, String> entry : entrySet) {
                context.write(entry.getKey(), NullWritable.get());
                i++;
                if (i == topn) {
                    return;
                }

            }
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        //向mapreduce内部各组件传递参数
        conf.set("movie.hot.top.n", args[0]);

        //conf.set("mapreduce.framework.name", "local");
        Job job = Job.getInstance(conf);
        //要执行的jar文件
        job.setJarByClass(HotMoviesTopN.class);
        //指定mapper类和reducer类
        job.setMapperClass(HotMoviesTopNMapper.class);
        job.setReducerClass(HotMoviesTopNReducer.class);
        //指定mapper和reducer的输出的key和value的数据类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(SortBean.class);
        job.setOutputValueClass(NullWritable.class);
        //指定要解析的文件路径
        FileInputFormat.setInputPaths(job, new Path(args[1]));
        //指定输出的文件路径
        FileOutputFormat.setOutputPath(job, new Path(args[2]));

        //设置reduce任务个数
        job.setNumReduceTasks(1);
        // 向yarn提交job
        boolean res = job.waitForCompletion(true);

        System.exit(res ? 0 : 1);
    }

}
